package com.google.code.jgib.growl.requests;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.google.code.jgib.growl.IGntpMessage;

public abstract class AbstractGntpRequest implements IGntpMessage {
	
	protected boolean ok=true;
	protected String requestString = "GNTP/1.0 ";

	@Override
	public boolean isRequest() {
		return true;
	}

	@Override
	public boolean isResponse() {
		return false;
	}

	@Override
	public boolean isOk() {
		return ok;
	}

	@Override
	public boolean isError() {
		return !ok;
	}

	@Override
	public String getRawGntpMessage() {
		return requestString;
	}
	
	public void doSendRequest(String host, int port){
		String responseString = "";
		Socket s = null;
		try {
			s = new Socket(host, port);
			s.getOutputStream().write(requestString.getBytes());
			InputStream response = s.getInputStream();
			int c;
			while((c = response.read()) != -1){
				responseString += (char)c;
			}
			System.out.println(responseString);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
